﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.TrackedRigidbody2D
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using UnityEngine;

#nullable disable
namespace Netick.Unity;

public class TrackedRigidbody2D : TrackedRigidbodyBase
{
  internal Rigidbody2D Body;
  internal NetworkSandbox Sandbox;
  internal Vector3 Pos;
  internal float Rot;
  internal Vector2 Vel;
  internal float AngVel;
  private Vector3 FromPos;
  private Quaternion FromRot;
  private Vector3 ToPos;
  private Quaternion ToRot;
  private bool _IsKinematic;

  public override bool IsKinematic
  {
    get => this._IsKinematic;
    internal set
    {
      this._IsKinematic = value;
      this.Body.isKinematic = value;
    }
  }

  internal override void Init(NetworkSandbox sandbox)
  {
    this.Body = this.GetComponent<Rigidbody2D>();
    this.Sandbox = sandbox;
    this.Pos = (Vector3) this.Body.position;
    this.Rot = this.Body.rotation;
    this.Vel = this.Body.velocity;
    this.AngVel = this.Body.angularVelocity;
    this.FromPos = this.transform.position;
    this.FromRot = this.transform.rotation;
    this.ToPos = this.transform.position;
    this.ToRot = this.transform.rotation;
    this.IsKinematic = this.Body.isKinematic;
  }

  internal override void PreRollback()
  {
    if (!this.Body.gameObject.activeInHierarchy)
      return;
    this.Pos = (Vector3) this.Body.position;
    this.Rot = this.Body.rotation;
    this.Vel = this.Body.velocity;
    this.AngVel = this.Body.angularVelocity;
    this.Body.isKinematic = true;
  }

  internal override void PostResimulation()
  {
    if (!this.Body.gameObject.activeInHierarchy)
      return;
    this.Body.isKinematic = this._IsKinematic;
    if ((double) (this.Pos - this.transform.position).magnitude > 9.9999997473787516E-05 | (double) Mathf.Abs(this.Body.rotation - this.Rot) > 1.0 / 1000.0)
    {
      this.Body.position = (Vector2) this.Pos;
      this.Body.rotation = this.Rot;
    }
    if ((double) (this.Vel - this.Body.velocity).sqrMagnitude <= 1.0 / 1000.0 && (double) Mathf.Abs(this.AngVel - this.Body.angularVelocity) <= 1.0 / 1000.0)
      return;
    this.Body.velocity = this.Vel;
    this.Body.angularVelocity = this.AngVel;
  }

  internal override void PostSimulation()
  {
    if (!this.Body.gameObject.activeInHierarchy)
      return;
    this.FromPos = this.ToPos;
    this.FromRot = this.ToRot;
    this.ToPos = this.transform.position;
    this.ToRot = this.transform.rotation;
  }

  internal override void Interpolate(float alpha)
  {
    if (!this.Body.gameObject.activeInHierarchy)
      return;
    this.transform.position = Vector3.Lerp(this.FromPos, this.ToPos, alpha);
    this.transform.rotation = Quaternion.Lerp(this.FromRot, this.ToRot, alpha);
  }

  public override void DestroyForNetick()
  {
    this.Sandbox.LocalRigidbodyFixer.Rigidbodies.Remove((TrackedRigidbodyBase) this);
  }

  private void OnDestroy()
  {
    this.Sandbox.LocalRigidbodyFixer.Rigidbodies.Remove((TrackedRigidbodyBase) this);
  }
}
